

package cn.yangliu.config.init;

import cn.yangliu.entity.Dictionary;
import cn.yangliu.init.InitHandler;
import cn.yangliu.repository.DictionaryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * the type of com-tool
 *
 * <p>
 * .
 *
 * @author 杨柳
 * @date 2021-09-28
 */
@Component
public class DictionaryInitHandler implements InitHandler<Dictionary> {

    @Autowired
    private DictionaryRepository repository;

    /**
     * 插入所有数据
     *
     * @param dataList 数据字典信息
     */
    @Override
    public void persistAll(List<Dictionary> dataList) {
        List<Dictionary> persisit = new ArrayList<>();
        for (Dictionary dictionary : dataList) {
            Optional<Dictionary> optional = repository.findById(dictionary.getId());
            if (!optional.isPresent()) {
                persisit.add(dictionary);
            }
        }

        if (!persisit.isEmpty()) {
            repository.saveAll(persisit);
        }
    }

    /**
     * 创建bean
     *
     * @param row 行数据
     * @return entity对象
     */
    @Override
    public Dictionary createBean(Object row) {
        return new Dictionary();
    }
}
